package com.skivingcloud.workflow.controller;


import java.util.Map;

import com.skivingcloud.admin.annotation.ModifyAnnotaionFactory;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.workflow.entity.Delegation;
import com.skivingcloud.workflow.service.IDelegationService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 转授权表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/workflow/delegation")
public class DelegationController {
	private static final Logger log = LoggerFactory.getLogger(DelegationController.class);

	@Autowired
	private IDelegationService delegationService;

	/**
	 * 授权信息
	 *
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		Delegation delegation = delegationService.findById(id);
		return R.ok().put("delegation", delegation);
	}

	/**
	 * 添加授权记录
	 *
	 * @param delegation
	 * @return
	 */
	@PostMapping("/addDelegation")
	@UserOperateLog(module = "工作流", methods = "addDelegation", description = "添加授权记录", functionNo = "104001")
	public R addDelegation(@RequestBody Delegation delegation) {
		try {
			delegationService.addDelegation(delegation);
			return R.ok();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return R.error("添加授权记录失败");
		}
	}

	/**
	 * 修改授权记录
	 */
	@PostMapping("/updateDelegation")
	@UserOperateLog(module = "工作流", methods = "updateDelegation", description = "修改授权记录", functionNo = "104002")
	public R updateDelegation(@RequestBody Delegation delegation) {
		ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "updateDelegation",
				"bussid", delegation.getId(), delegation.getClass());
		try {
			delegationService.updateDelegation(delegation);
			return R.ok();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return R.error("修改授权记录失败");
		}
	}

	/**
	 * 查询授权列表
	 */
	@GetMapping("/list")
	public R getList(@RequestParam Map<String, Object> params) {
		PageUtils page = delegationService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 删除授权列表
	 */
	@UserOperateLog(module = "工作流", methods = "deleteDelegation", description = "删除授权列表")
	@PostMapping("/delete")
	public R deleteDelegation(@RequestBody String[] ids) {
		try {
			delegationService.deleteDelegation(ids);
			return R.ok();
		} catch (BusinessException e) {
			log.error(e.getMessage(), e);
			return R.error(e.getMessage());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return R.error("删除授权列表失败");
		}
	}

	@PostMapping("/sendAttachmentMail")
	public R send(String subject) {
		System.out.println(subject);
		return R.ok();
	}
}
